package com.hivekion.workflow.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hivekion.workflow.converter.CustomBpmnJsonConverter;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.converter.CallActivityXMLConverter;
import org.activiti.bpmn.converter.UserTaskXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.io.StringReader;

public class BpmnConverterUtil {
      public static String converterJsonToWebXml(String jsonStr) {
	    ObjectMapper objectMapper = new ObjectMapper();
	    JsonNode jsonNode = null;
	    try {
		  jsonNode = objectMapper.readTree(jsonStr);
	    } catch (IOException e) {
		  e.printStackTrace();
	    }
	    CustomBpmnJsonConverter customBpmnJsonConverter = new CustomBpmnJsonConverter();
	    BpmnModel bpmnModel = customBpmnJsonConverter.convertToBpmnModel(jsonNode);
	    // 如果没有Processes,认为是一个空流程
	    if (bpmnModel.getProcesses().isEmpty()) {
		  return "";
	    }
	    BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
	    byte[] bytes = bpmnXMLConverter.convertToXML(bpmnModel);
	    return new String(bytes);
      }

      public static String converterJsonToXml(String jsonStr) {
	    ObjectMapper objectMapper = new ObjectMapper();
	    JsonNode jsonNode = null;
	    try {
		  jsonNode = objectMapper.readTree(jsonStr);
	    } catch (IOException e) {
		  e.printStackTrace();
	    }
	    BpmnJsonConverter bpmnJsonConverter = new BpmnJsonConverter();
	    BpmnModel bpmnModel = bpmnJsonConverter.convertToBpmnModel(jsonNode);
	    // 如果没有Processes,认为是一个空流程
	    if (bpmnModel.getProcesses().isEmpty()) {
		  return "";
	    }
	    BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
	    byte[] bytes = bpmnXMLConverter.convertToXML(bpmnModel);
	    return new String(bytes);
      }

      public static JsonNode converterXmlToJson(String xml) {
	    // 创建转换对象
	    BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
	    // XMLStreamReader读取XML资源
	    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
	    StringReader stringReader = new StringReader(xml);
	    XMLStreamReader xmlStreamReader = null;
	    try {
		  xmlStreamReader = xmlInputFactory.createXMLStreamReader(stringReader);
	    } catch (XMLStreamException e) {
		  e.printStackTrace();
	    }
	    BpmnXMLConverter.addConverter(new UserTaskXMLConverter());
	    BpmnXMLConverter.addConverter(new CallActivityXMLConverter());
	    // 把xml转换成BpmnModel对象
	    BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(xmlStreamReader);
	    // 创建转换对象
	    BpmnJsonConverter bpmnJsonConverter = new BpmnJsonConverter();
	    // 把BpmnModel对象转换成json
	    JsonNode jsonNodes = bpmnJsonConverter.convertToJson(bpmnModel);
	    return jsonNodes;
      }
}
